package mapper;

import dao.BaseDao;
import entity.hotel_order;
import entity.hotel_room;
import entity.hotel_tenant;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class HotelOrderMapper {
    BaseDao baseDao = new BaseDao();

    public int insertOrder(hotel_order hotel_order) {
        SimpleDateFormat sdf =new SimpleDateFormat("dd-M月-yyyy");

        String sql = "insert into hotel_order values(hotel_order_seq.nextval,?,?,?,?,?)";
        Object[] params={hotel_order.getRoom_no(),hotel_order.getRoom_price(),hotel_order.getEmployee_id()
        ,sdf.format(hotel_order.getOrder_time()),hotel_order.getOrder_remarks()};
        return baseDao.update(sql,params);
    }

    public int insertTenant(hotel_tenant hotel_tenant) {
        String sql = "insert into hotel_tenant values(?,?,?,?,?)";
        Object[] params={hotel_tenant.getTenant_name(),hotel_tenant.getTenant_sex(),hotel_tenant.getTenant_id()
                , hotel_tenant.getTenant_tel(),hotel_tenant.getRoom_no()};
        return baseDao.update(sql,params);
    }

    public List<hotel_room> selectByClass(String room_class) {
        String sql = "select * from hotel_room where room_class=? and room_status=0";
        Object []params = {room_class};
        ResultSet rs = baseDao.query(sql,params);
        if (rs==null){
            return null;
        }else {
            List<hotel_room> roomList = new ArrayList<>();
            try {
                while(rs.next()){
                    String room_no = rs.getString("room_no");
                    String roomClass = rs.getString("room_class");
                    int room_price = rs.getInt("room_price");
                    String room_status = rs.getString("room_status");
                    hotel_room hotel_room = new hotel_room(room_no,roomClass,room_price,room_status);
                    roomList.add(hotel_room);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return roomList;
        }
    }

    public List<hotel_order> selectAllOrder(){
        String sql = "select * from hotel_order";
        Object[] params = {};
        ResultSet rs = baseDao.query(sql,params);
        if(rs==null){
            return null;
        }else {
            List<hotel_order> orderList = new ArrayList<>();
            try {
                while(rs.next()){
                    String order_id = rs.getString("order_id");
                    String room_no = rs.getString("room_no");
                    int room_price = rs.getInt("room_price");
                    String employee_id = rs.getString("employee_id");
                    Date order_time = rs.getDate("order_time");
                    String order_remarks = rs.getString("order_remarks");
                    hotel_order hotel_order = new hotel_order(order_id,room_no,room_price,employee_id,order_time,order_remarks);
                    orderList.add(hotel_order);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return orderList;
        }
    }

    public List<hotel_tenant> selectAllTenant(){
        String sql = "select * from hotel_tenant";
        Object[] params = {};
        ResultSet rs = baseDao.query(sql,params);
        if(rs==null){
            return null;
        }else {
            List<hotel_tenant> tenantList = new ArrayList<>();
            try {
                while(rs.next()){
                    String tenant_name = rs.getString("tenant_name");
                    String tenant_sex = rs.getString("tenant_sex");
                    String tenant_id = rs.getString("tenant_id");
                    String tenant_tel = rs.getString("tenant_tel");
                    String room_no = rs.getString("room_no");
                    hotel_tenant hotel_tenant = new hotel_tenant(tenant_name,tenant_sex,tenant_id,tenant_tel,room_no);
                    tenantList.add(hotel_tenant);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return tenantList;
        }
    }

    public int ChangeRoomStatus(String room_no,String flag) {
        String sql = "update hotel_room set room_status=? where room_no=?";
        Object []params ={flag,room_no};
        int code = baseDao.update(sql,params);
        return code;
    }

    public List<hotel_room> selectAllRoom() {
        String sql = "select * from hotel_room";
        Object[] params = {};
        ResultSet rs = baseDao.query(sql,params);
        if(rs==null){
            return null;
        }else {
            List<hotel_room> roomList = new ArrayList<>();
            try {
                while(rs.next()){
                    String room_no = rs.getString("room_no");
                    String room_class = rs.getString("room_class");
                    int room_price = rs.getInt("room_price");
                    String room_status = rs.getString("room_status");
                    hotel_room hotel_room = new hotel_room(room_no,room_class,room_price,room_status);
                    roomList.add(hotel_room);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return roomList;
        }
    }

    public int deleteTenant(String room_no) {
        String sql = "delete from hotel_tenant where room_no=?";
        Object[] params = {room_no};
        int code = baseDao.update(sql,params);
        return code;
    }

    public hotel_tenant selectTenantByRoomNo(String room_no) {
        String sql = "select * from hotel_tenant where room_no=?";
        Object[] params = {room_no};
        ResultSet rs = baseDao.query(sql,params);
        if(rs==null){
            return null;
        }else {
            hotel_tenant hotel_tenant=null;
            try {
                if(rs.next()){
                    String tenant_name = rs.getString("tenant_name");
                    String tenant_sex = rs.getString("tenant_sex");
                    String tenant_id = rs.getString("tenant_id");
                    String tenant_tel = rs.getString("tenant_tel");
                    hotel_tenant = new hotel_tenant(tenant_name,tenant_sex,tenant_id,tenant_tel,room_no);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return hotel_tenant;
        }
    }
}
